Skip to content

Refactor: Convert chunks_exact[_mut] iterators to as_chunks[_mut] iterators#2765

Merged
197g merged 15 commits intoimage-rs:mainfrom
okaneco:convert_to_as_chunks
Feb 12, 2026
Merged

Refactor: Convert chunks_exact[_mut] iterators to as_chunks[_mut] iterators#2765
197g merged 15 commits intoimage-rs:mainfrom
okaneco:convert_to_as_chunks

Conversation

@okaneco
Copy link
Contributor

@okaneco okaneco commented Feb 11, 2026

  • Use the as_chunks[_mut] iterators when the chunk size is constant.

There's a clippy lint for this in the works but it's not merged yet.


This is a refactoring PR with no functional changes intended.

The newer chunk iterators give the compiler more information to potentially eliminate bounds checks or autovectorize/unroll beyond what the old style iterators could do. There are still cases today, with the latest LLVM upgrade, where simple-looking chunks_exact code doesn't autovectorize until upgraded to use as_chunks iterators. The new iterators are also more resilient to autovectorizer regressions such as the ones we saw starting in 1.87 (LLVM 20) which were fixed recently with the bump to LLVM 22.

This allowed for some other local cleanups, such as:

  • Cleaning up several try_into().unwrap() array casts
  • Removing the need for some byteorder read/write calls
  • Changing some small function signatures to take arrays as input arguments instead of slices

@okaneco okaneco force-pushed the convert_to_as_chunks branch 2 times, most recently from d9435e4 to 031cb3b Compare February 11, 2026 22:26
@okaneco okaneco force-pushed the convert_to_as_chunks branch from 031cb3b to 1d1bb95 Compare February 11, 2026 22:37
Copy link
Member

@197g 197g left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than a few style nits, this looks good. Thank you. Could you have a cursory look if you see the redundant &mut chunk[..CN] pattern with other constants elsewhere? Nothing crucial but if you find them then the style could be cleared up.

for (x, dst) in (image_size.width..(image_size.width + pad_w))
.zip(row_buffer.as_chunks_mut::<N>().0.iter_mut().rev())
{
let old_x = x.max(0).min(image_size.width - 1);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering why max(0) on usize isn't triggering clippy::unnecessary_min_or_max.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe .max(0) is a clippy bug and can be safely removed. Interestingly, this presents in my original code for years.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just pushed a commit to remove it.

let bias_y = range.bias_y as i32;
let bias_uv = range.bias_uv as i32;
let y_iter = y_plane.chunks_exact(2);
let rgb_chunks = rgba.chunks_exact_mut(CHANNELS * 2);
Copy link
Contributor

@awxkee awxkee Feb 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting fact: Rust with LLVM was always bad in autovectorizing YUV chroma subsampling.
Now, with as_chunks[_mut] it can at least autovectorize YUV encoding with chroma subsampling but not decoding.

Remove pattern of using slicing to do one bounds check now that arrays
are being used.
Break array::map function into its own line
Write directly into array instead of indexing since the types match
Remove no-op `max(0)` call on a usize
@okaneco okaneco force-pushed the convert_to_as_chunks branch from ca89210 to e6dad12 Compare February 12, 2026 21:46
@197g 197g merged commit 8b28f75 into image-rs:main Feb 12, 2026
32 checks passed
@197g
Copy link
Member

197g commented Feb 12, 2026

Thank you!

@okaneco okaneco deleted the convert_to_as_chunks branch February 12, 2026 23:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants